<template>
  <div class="">
    <h1>学员管理</h1>

    <el-form :inline="true" :model="search" class="demo-form-inline">
      <el-form-item label="姓名">
        <el-input v-model="search.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="电话号码">
        <el-input v-model="search.mobile" placeholder="电话"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="searchs">查询</el-button>
        <el-button type="primary" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" @click="removeall">删除选中</el-button>

    <el-table
      ref="box"
      :data="reslist"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="change"
      border
      stripe
    >
      <el-table-column type="selection" width="55"> </el-table-column>

      <el-table-column label="姓名" width="120">
        <template slot-scope="scope">
          <!-- {{ scope.row }} -->
          <!-- <img :src="scope.row.avatar" alt="" /> -->
          <el-avatar size="large" :src="scope.row.avatar"></el-avatar>
          <span>{{ scope.row.nickname }}</span>
        </template>
        <!-- scope.row代表这一行数据 就相当于是item
         scope.$index就是下标 -->
      </el-table-column>
      <el-table-column
        prop="mobile"
        label="电话号码"
        show-overflow-tooltip
        width="150"
      >
      </el-table-column>

      <el-table-column label="状态" width="100">
        <template slot-scope="scope">
          {{ scope.row.status ? "启用" : "禁用" }}
        </template>
      </el-table-column>

      <el-table-column label="创建时间" width="180">
        <template slot-scope="scope">
          {{ new Date(scope.row.created_at * 1000).toLocaleString() }}
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" round>删除</el-button>
          <el-button size="mini" round @click="edit(scope.row, scope.$index)"
            >编辑</el-button
          >
          <el-button size="mini" round v-if="scope.row.status">禁用</el-button>
          <el-button size="mini" round v-else>启用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 在elementui的表格中 如果是直接展示的数据 直接用prop展示 但是如果这个数据不是直接展示的就要用插槽 -->

    <el-pagination
      background
      layout="total,prev, pager, next,jumper"
      :total="list.length"
      :page-size="pagesize"
      :current-page="pagenum"
      @current-change="changePage"
    >
    </el-pagination>

    <!-- :page-size="pagesize"  定义每页多少条数据
      :current-page="pagenum" 当前在第几页
      @current-change="changePage" 页码改变会触发这个事件 -->
    <el-dialog title="编辑" :visible.sync="show">
      <el-form :model="form">
        <el-form-item label="姓名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="电话号码">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      //列表数据
      copylist: [],
      //备份的数据 为了实现搜索
      show: false,
      //弹框的显示隐藏
      form: {
        name: "",
        mobile: "",
      },
      //编辑弹框的 v-model
      editIndex: -1,
      //编辑的下标
      search: {
        name: "",
        mobile: "",
      },
      //搜索的表单的v-model
      result: [],
      //复选框选中的内容
      pagesize: 3,
      pagenum: 4,
    };
  },
  mounted() {
    this.$axios.get("list.json").then((res) => {
      console.log(res);
      this.list = res.data;
      this.copylist = res.data;
    });
  },
  methods: {
    change(val) {
      console.log(val);
      //这个事件点击复选框的时候会触发 默认的参数val代表 被选中的数据 是一个数组存的是整条数据
      this.result = val;
    },

    edit(item, index) {
      this.editIndex = index;
      this.show = true;
      this.form.name = item.nickname;
      this.form.mobile = item.mobile;
    },
    //点击编辑 保存下标 回填数据
    update() {
      this.show = false;
      this.list[this.editIndex].nickname = this.form.name;
      this.list[this.editIndex].mobile = this.form.mobile;
    },
    // 点击确认 修改数据
    searchs() {
      this.list = this.copylist.filter((item) => {
        return (
          item.nickname.includes(this.search.name) &&
          item.mobile.includes(this.search.mobile)
        );
      });
    },
    //搜索 根据姓名和手机号进行搜索
    reset() {
      this.list = this.copylist;
      this.search.name = "";
      this.search.mobile = "";
    },
    //重置
    removeall() {
      this.list = this.list.filter((item) => {
        return this.result.includes(item) == false;
      });
    },
    //选中删除
    changePage(val) {
      //这个事件 改变当前页码的时候会触发 默认的参数是 当前的页码
      this.pagenum = val;
      //把当前的页码存给pagenum
    },
  },
  computed: {
    reslist() {
      return this.list.slice(
        (this.pagenum - 1) * this.pagesize,
        this.pagenum * this.pagesize
      );
      //用计算属性计算要截取的json数据
      // slice （从哪儿开始截，截到哪里） 包头不包尾
      // 1 3  012 0,3
      // 2 3  345 3,6
      // 3 3 678  6,9
      // 4 3 91011
    },
  },
  components: {},
};
</script>

<style lang="scss"></style>
